#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === ROM Auto Patcher ===
function rompatcher()
{
  # Usage: rompatcher (Helper menu access to *patcher functions)

  # Variables
  local cnt=0;
  local key;
  local line_function;
  local line_usage;
  local list_functions=('');
  local roomservice_xml;
  local search_patcher;

  # Reset inputs
  key='';
  search_patcher='';

  # Detect the existence of roomservice.xml
  roomservice_xml=$(gettop)/.repo/local_manifests/roomservice.xml;
  if [ -f "${roomservice_xml}" ]; then
    search_patcher=$(grep "ROM Patcher: '" "${roomservice_xml}" \
                   | sed "s/.* '\(.*\)' .*/\1/");

    # Notify found rompatcher function
    if [ ! -z "${search_patcher}" ]; then
      echo '';
      echo -e  " \e[1;32mDetected rompatcher to use : '${search_patcher}'\e[0m ";
    fi;
  fi;

  # Function header
  echo '';
  echo -e " \e[1;37m[ Android Development Shell Tools - ROM Patchers ]\e[0m";
  echo '';

  # Display *patcher menu
  for line_function in $(typeset -F | grep 'patcher$' | cut -d' ' -f 3 \
                       | grep -v '^rompatcher$' \
                       | grep -v '^androidprojectpatcher$'); do

    # Parse usage
    line_usage=$(grep -a "Usage: ${line_function} " "${ANDROID_DEVELOPMENT_SHELL_TOOLS_SOURCES[@]}");
    if [ -z "${line_usage}" ]; then
      continue;
    fi;
    line_usage=${line_usage#*\(};
    line_usage=${line_usage%\)*};
    list_functions+=("${line_function}");

    # Item choice
    cnt=$((cnt + 1));
    if [ "${line_function}" = "${search_patcher}" ]; then
      key=${cnt};
    fi;
    echo -e "  \e[1;33m${cnt}: \e[1;37m${line_function} \e[1;32m[${line_usage}]\e[0m";

  done;
  echo '';

  # Request user input
  echo -n " > Command to run [1-${cnt}] : ";
  if [ -z "${key}" ]; then
    read -r key;
  else
    echo "${key}";
  fi;
  echo '';

  # Execute requested function
  ${list_functions[${key}]} "${@}";
}

# === ROM Auto Rebaser ===
function romrebaser()
{
  # Usage: romrebaser (Helper menu access to *rebaser functions)

  # Variables
  local cnt=0;
  local key;
  local line_function;
  local line_usage;
  local list_functions=('');
  local search_rebaser;

  # Reset inputs
  key='';
  search_rebaser='';

  # Detect the existence of roomservice.xml
  if [ -z "${1}" ] && [ -f '.repo/local_manifests/roomservice.xml' ]; then
    search_rebaser=$(grep "ROM Rebaser: '" '.repo/local_manifests/roomservice.xml' \
                   | sed "s/.* '\(.*\)' .*/\1/");

    # Notify found romrebaser function
    if [ ! -z "${search_rebaser}" ]; then
      echo '';
      echo -e  " \e[1;32mDetected romrebaser to use : '${search_rebaser}'\e[0m ";
    fi;
  fi;

  # Function header
  echo '';
  echo -e " \e[1;37m[ Android Development Shell Tools - ROM Rebasers ]\e[0m";
  echo '';

  # Display *rebaser menu
  for line_function in $(typeset -F | grep 'rebaser$' | cut -d' ' -f 3 \
                       | grep -v '^romrebaser$' \
                       | grep -v '^androidprojectrebaser$'); do

    # Parse usage
    line_usage=$(grep -a "Usage: ${line_function} " "${ANDROID_DEVELOPMENT_SHELL_TOOLS_SOURCES[@]}");
    if [ -z "${line_usage}" ]; then
      continue;
    fi;
    line_usage=${line_usage#*\(};
    line_usage=${line_usage%\)*};
    list_functions+=("${line_function}");

    # Item choice
    cnt=$((cnt + 1));
    if [ "${line_function}" = "${search_rebaser}" ]; then
      key=${cnt};
    fi;
    echo -e "  \e[1;33m${cnt}: \e[1;37m${line_function} \e[1;32m[${line_usage}]\e[0m";

  done;
  echo '';

  # Request user input
  echo -n " > Command to run [1-${cnt}] : ";
  if [ -z "${key}" ]; then
    read -r key;
  else
    echo "${key}";
  fi;
  echo '';

  # Execute requested function
  ${list_functions[${key}]} "${@}";
}
